﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

namespace DeJC.BLL.Utilities {
    public static class Encrypt {
        public static string Md5Encrypt(string phrase) {
            var encoder=new UTF8Encoding();
            var md5hasher=new MD5CryptoServiceProvider();
            byte[] hashedDataBytes=md5hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        public static string Sha1Encrypt(string phrase) {
            var encoder=new UTF8Encoding();
            var sha1hasher=new SHA1CryptoServiceProvider();
            byte[] hashedDataBytes=sha1hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        public static string Sha256Encrypt(string phrase) {
            var encoder=new UTF8Encoding();
            var sha256hasher=new SHA256Managed();
            byte[] hashedDataBytes=sha256hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        public static string Sha384Encrypt(string phrase) {
            var encoder=new UTF8Encoding();
            var sha384hasher=new SHA384Managed();
            byte[] hashedDataBytes=sha384hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        public static string Sha512Encrypt(string phrase) {
            var encoder=new UTF8Encoding();
            var sha512hasher=new SHA512Managed();
            byte[] hashedDataBytes=sha512hasher.ComputeHash(encoder.GetBytes(phrase));
            return ByteArrayToString(hashedDataBytes);
        }
        public static string ByteArrayToString(byte[] inputArray) {
            var output=new StringBuilder("");
            for(int i=0; i<inputArray.Length; i++) {
                output.Append(inputArray[i].ToString("X2"));
            }
            return output.ToString();
        }
    }
}
